console.log('-------------- search -----------------')
const organization = this.pageObj.getApiWidgetByAliasName('tree_organization');
const search_input = this.pageObj.getApiWidgetByAliasName('search_input');
const three = this.pageObj.getApiWidgetByAliasName('三维组件');
const type = this.pageObj.getApiWidgetByAliasName('type_selection');
const select = type.widgetObj.getChecked()?.value;
const prefix = this.pageObj.getVariables().find(v => v.name === 'environmentType')?.value || '';
const text = $(search_input.__box).find('.input-filter').val();

function setTitle ({ typeCode, communicationName, personName, username, userId } = {}) {
    let title = '';

    if (typeCode === 'dispatchDesk') title += '调度台';

    title += (communicationName || username || personName);

    if (userId) title += `(${userId})`;

    return title
}

const random = (num = 10) => parseInt(Math.random() * Math.pow(10, num))
function resetMember (member, index) {
    return {
        ...member,
        type: '1',
        title: `${member.personName || member.communicationName || `匿名${index}`}(${member.account})`,
        username: `${member.personName || member.communicationName || `匿名${index}`}`,
        userId: member.account,
        treeId: member.id || member.personId,
    }
}
function breakOrgList(item) {
    const dispatchLength = item.dispatchList?.length
    item.title = `${item.orgName}[${item.count}]`;
    item.id = item.orgId;
    item.treeId = item.orgId;
    if (item.children) {
        item.children = item.children.map(breakDataSource);
        item.type = '0'
    }

    if (dispatchLength) {
        const id = `${random(10)}-${random(10)}`;
        item.children.unshift({
            id,
            treeId: id,
            title: `调度台[${dispatchLength}]`,
            children: item.dispatchList.map(resetMember),
        })
    }

    if (item.personList) {
        item.personList.map((member, index) => {
            item.children.push(resetMember(member, index))
        });
    }

    return item
}
function breakDataSource(item) {
    if(item.pointId || item.communicationName || item.personName) {
        item.id = item.personId;
        item.userId = item.account || item.communicationAccount;
        item.username = item.communicationName || item.personName;
        item.title = setTitle(item);
        item.treeId = item.userId;
        item.disabled = true
    } else if(item.orgId || item.orgName) {
        item.title = item.orgName;
        item.id = item.orgId;
        item.treeId = item.orgId;
    } else if (item.deptId || item.deptName) {
        item.title = item.deptName;
        item.id = item.deptId;
        item.treeId = item.deptId;
    } else if (item.distributionGroupId || item.distributionGroupName) {
        item.title = item.distributionGroupName;
        item.id = item.distributionGroupId;
        item.treeId = item.distributionGroupId;
    } else if (item.equipmentTypeName || item.equipmentTypeCode) {
        item.title = item.equipmentTypeName;
        item.id = item.equipmentTypeCode;
        item.treeId = item.equipmentTypeCode;
    }

    if (item.orgList) item.children = item.orgList.map(breakDataSource);
    if (item.groupList) item.children = item.groupList.map(breakDataSource);
    if (item.personList) item.children = item.personList.map(breakDataSource);
    if (item.accountData) item.children = item.accountData.map(breakDataSource);
    if (item.equipmentType) item.children = item.equipmentType.map(breakDataSource);

    return item
}

let fetchUrl = `/${prefix}Communication/pubCommunicationAccount/getOrgListByName`;
let option = {}

if (window.selCheckedNum === 3) {
    fetchUrl = `/${prefix}Communication/pubCommunicationAccount/getAllAccountByEquipment`
    option = { method: 'POST', headers: {'Content-Type': 'application/json'},  body: "{}" }

    if (text) option.body = JSON.stringify({ communicationName: text })
} else {
    if (window.selCheckedNum === 1) {
        fetchUrl = `/${prefix}Communication/pubCommunicationAccount/getOrgListByName`
    } else if (window.selCheckedNum === 2) {
        fetchUrl = `/${prefix}Communication/pubCommunicationAccount/getGroupListByName`
    }

    if (text) fetchUrl += `?communicationName=${text}`
}

fetch(fetchUrl, option).then(res => res.json()).then(res => {
    const { result, resultCode } = res;
    if (resultCode === '000000') {
        let dataSource = [];

        if (window.selCheckedNum === 1) {
            dataSource = res.result.map(breakOrgList);
        } else {
            dataSource = res.result.map(breakDataSource);
        }

        var dataSourceSelect;
        const artemis = three.widgetObj.artemis;
        if (select === 'all') {
            dataSourceSelect = artemis.resetUsersStatus(dataSource);
        } else {
            dataSourceSelect = artemis.filterUsersOnline(dataSource);
        }

        organization.widgetObj._option.dataSource = dataSourceSelect;
        organization.widgetObj.render();
        console.log(res, dataSourceSelect)
    }
}).catch(error => console.error(error))
